<template>
  <div>
    <div class="cate-wrap">
      <div class="cate-left">
        <div class="cates">
          <div
            v-for="(cate, index) in cates"
            :key="cate.id"
            @click="switchTab(index)"
            :class="['cate', {
              active: index === num
            }]">{{cate.name}}</div>
        </div>
      </div>
      <div class="cate-right">
        <div class="items">
      <div class="item" v-for="item in items" :key="item.id">
        <img :src="item.pic" alt="" class="item-left">
        <div class="item-info">
          <h3 class="name">{{item.name}}</h3>
          <p class="desc">
            {{ item.characteristic ? item.characteristic: '暂无描述' | filterDesc}}
          </p>
          <div class="item-action">
            <p class="price">
              {{ item.minPrice | currency }}
            </p>
            <van-icon class-prefix="icon" color="#f88" size="22" name="jia"></van-icon>
          </div>
        </div>
      </div>
    </div>
      </div>
    </div>
    <tabbar/>
  </div>
</template>

<script>
import Tabbar from '../../components/Tabbar.vue'
import { fetchCates, fetchItems } from '@api'
export default {
  data () {
    return {
      cates: [],
      items: [],
      num: 0
    }
  },
  created () {
    this.fetchCates()
  },
  methods: {
    switchTab (index) {
      // 点击切换分类
      this.num = index
      this.fetchItems()
    },
    fetchCates () {
      // 请求所有的分类
      fetchCates().then(res => {
        if (res.data.code === 0) {
          this.cates = res.data.data
          this.fetchItems()
        }
      })
    },
    fetchItems () { // 请求商品数据
      this.items = []
      fetchItems({
        categoryId: this.cates[this.num].id
      }).then(res => {
        if (res.data.code === 0) {
          this.items = res.data.data.result
        }
      })
    }
  },
  filters: {
    filterDesc (v, len = 10) {
      if (v.length > 10) {
        return v.substr(0, len) + '...'
      } else {
        return v
      }
    },
    currency (v, sign = '￥') {
      return sign + v
    }
  },
  components: {
    Tabbar
  }
}
</script>

<style lang="scss" scoped>
.cate-wrap{
  position: fixed;
  left:0;
  right:0;
  top:0;
  bottom: 50px;
  display: flex;
  .cate-left{
    width: 110px;
    border-right: 1px solid #ccc;
    height: 100%;
    background-color: #f1f1f1;
    overflow-y: auto;
    .cate{
      line-height: 40px;
      font-size: 14px;
      text-align: center;
      color:#333;
      &.active{
        border-left: 3px solid #f66;
        background-color: #fff;
      }
    }
  }
  .cate-right{
    flex:1;
    height: 100%;
    overflow-y: auto;
    .items{
  .item{
    display: flex;
    &-left{
      width: 120px;
      height: 120px;
      
    }
    &-info{
      flex:1;
      padding:0 10px;
      box-sizing: border-box;
      color: #333;
      .name{
        font-size: 16px;
         color: #333;
         line-height: 30px;
      }
      .desc{
         font-size: 14px;
         color: #666;
      }
      .item-action{
        display: flex;
        margin-top: 10px;
        justify-content: space-between;
        align-items: center;
        p.price{
          color:#f88;
        }
      }
    }
  }
}
  }
}
</style>
